/*
Copyright (c) 2008, Daniel Guerrero Martínez
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
	-Redistributions of source code must retain the above copyright notice, this
	list of conditions and the following disclaimer.
	-Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.
	-Neither the name of the <ORGANIZATION> nor the names of its contributors may
	be used to endorse or promote products derived from this software without
	specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
 */

import AudioVideo.Capturadora;
import Imagenes.BarraHerramientas;
import Imagenes.LienzoInterno;
import Imagenes.nuevo_lienzo;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
import AudioVideo.Reproductor;
import javax.swing.JOptionPane;


public class Principal extends javax.swing.JFrame {
    
    /** Creates new form Principal */
    public Principal() {
        escritorio = new JDesktopPane();
        this.getContentPane().add(escritorio,java.awt.BorderLayout.CENTER);
        herramientas = new BarraHerramientas();
        herramientas.show();
        escritorio.add(herramientas);
        initComponents();
        try {
                UIManager.setLookAndFeel(UIManager.getInstalledLookAndFeels()[1].getClassName());
                SwingUtilities.updateComponentTreeUI(this);
        } catch ( Exception excepcion ) {
                JOptionPane.showMessageDialog(null, "Error al cargar estilos visuales","Error",JOptionPane.ERROR_MESSAGE);
        }
        apariencias = UIManager.getInstalledLookAndFeels();
        for(int i = 0; i<apariencias.length;i++){
            javax.swing.JRadioButtonMenuItem nuevo = new javax.swing.JRadioButtonMenuItem(apariencias[i].getName());
            nuevo.addItemListener(new Manejador_estilos(apariencias[i],this));
            M_visual.add(nuevo);
            grupo_visual.add(nuevo);
        }
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo_visual = new javax.swing.ButtonGroup();
        Menu = new javax.swing.JMenuBar();
        M_Archivo = new javax.swing.JMenu();
        M_Nuevo_lienzo = new javax.swing.JMenuItem();
        M_abrir_imagen = new javax.swing.JMenuItem();
        M_sonido = new javax.swing.JMenuItem();
        B_capturar = new javax.swing.JMenuItem();
        M_visual = new javax.swing.JMenu();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        M_Archivo.setText("Archivo");

        M_Nuevo_lienzo.setText("Nuevo Lienzo");
        M_Nuevo_lienzo.addActionListener(formListener);
        M_Archivo.add(M_Nuevo_lienzo);

        M_abrir_imagen.setText("Abrir imagen");
        M_abrir_imagen.addActionListener(formListener);
        M_Archivo.add(M_abrir_imagen);

        M_sonido.setText("Abrir medio");
        M_sonido.addActionListener(formListener);
        M_Archivo.add(M_sonido);

        B_capturar.setText("Capturar");
        B_capturar.addActionListener(formListener);
        M_Archivo.add(B_capturar);

        Menu.add(M_Archivo);

        M_visual.setText("visual");
        Menu.add(M_visual);

        setJMenuBar(Menu);

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == M_Nuevo_lienzo) {
                Principal.this.nuevo_lienzo(evt);
            }
            else if (evt.getSource() == M_abrir_imagen) {
                Principal.this.abrir_imagen(evt);
            }
            else if (evt.getSource() == M_sonido) {
                Principal.this.abrir_sonido(evt);
            }
            else if (evt.getSource() == B_capturar) {
                Principal.this.capturar(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void nuevo_lienzo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nuevo_lienzo
         nuevo_lienzo nuevo = new nuevo_lienzo(this,true,escritorio, herramientas);
         nuevo.setVisible(true);
}//GEN-LAST:event_nuevo_lienzo

    private void abrir_imagen(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrir_imagen
        JFileChooser dig = new javax.swing.JFileChooser();
        dig.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "gif","png"));
        BufferedImage img = null;
        int resp = dig.showOpenDialog(this);
        if(resp == JFileChooser.APPROVE_OPTION){
            try {
                File f = dig.getSelectedFile();
                img = ImageIO.read(f);
                if(img != null){
                escritorio.add(new LienzoInterno(herramientas,img));
                }else{
                    JOptionPane.showMessageDialog(null, "Error al leer la imagen","Error",JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "Error al leer la imagen","Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_abrir_imagen

    private void abrir_sonido(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_abrir_sonido
        Reproductor r = new Reproductor();
        escritorio.add(r);
        r.setLocation(escritorio.getWidth()-r.getWidth(), 0);
    }//GEN-LAST:event_abrir_sonido

    private void capturar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_capturar
        Capturadora r = new Capturadora();
        escritorio.add(r);
        r.setLocation(escritorio.getWidth()-r.getWidth(), 0);
    }//GEN-LAST:event_capturar

    private class Manejador_estilos implements ItemListener {
       private UIManager.LookAndFeelInfo estilo;
       private JFrame padre;

       public Manejador_estilos( UIManager.LookAndFeelInfo S,JFrame P )
       {
          estilo = S;
          padre = P;
       }

       public void itemStateChanged( ItemEvent evento )
       {
            try {
                UIManager.setLookAndFeel(estilo.getClassName());
                SwingUtilities.updateComponentTreeUI(padre);
            } catch ( Exception excepcion ) {
                JOptionPane.showMessageDialog(null, "Error Al cargar estilos visuales","Error",JOptionPane.ERROR_MESSAGE);
            }
       }

   }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem B_capturar;
    private javax.swing.JMenu M_Archivo;
    private javax.swing.JMenuItem M_Nuevo_lienzo;
    private javax.swing.JMenuItem M_abrir_imagen;
    private javax.swing.JMenuItem M_sonido;
    private javax.swing.JMenu M_visual;
    private javax.swing.JMenuBar Menu;
    private javax.swing.ButtonGroup grupo_visual;
    // End of variables declaration//GEN-END:variables
    private JDesktopPane escritorio;
    private BarraHerramientas herramientas;
    private UIManager.LookAndFeelInfo apariencias[];
}
